package com.example.fan.airline_reservation.Adapter;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fan.airline_reservation.Model.Fight;
import com.example.fan.airline_reservation.R;
import com.example.fan.airline_reservation.ac_to_modifyfight;
import com.example.fan.airline_reservation.register;

import java.util.List;


public class FightAdapter extends RecyclerView.Adapter<FightAdapter.ViewHolder> {

    private List<Fight> mFightList;

    static class ViewHolder extends RecyclerView.ViewHolder{

        View fightView;

        TextView over24hour;/*是否要+1天*/
        TextView go_off;/*出发时间*/
        TextView departure;/*出发地*/
        TextView arrival_time;/*到达时间*/
        TextView termini;/*目的地*/
        TextView ac_price;/*机票价格*/
        TextView information;/*航空公司和机型信息*/
        TextView capacity;/*容量*/
        ImageView log_icon;/*航空公司图标*/
        TextView state;/*航班状态*/


        public ViewHolder(View view){
            super(view);
            fightView=view;
            over24hour=(TextView)view.findViewById(R.id.over24Hour);
            go_off=(TextView)view.findViewById(R.id.go_off);
            departure=(TextView)view.findViewById(R.id.departure);
            arrival_time=(TextView)view.findViewById(R.id.arrival_time);
            termini=(TextView)view.findViewById(R.id.termini);
            ac_price=(TextView)view.findViewById(R.id.ac_price);
            information=(TextView)view.findViewById(R.id.information);
            capacity=(TextView)view.findViewById(R.id.capacity);
            log_icon=(ImageView) view.findViewById(R.id.ac_logo);
            state=(TextView)view.findViewById(R.id.state);
        }

    }

    public FightAdapter(List<Fight> fightList){
        mFightList=fightList;
    }

    @NonNull
    @Override
    public FightAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        final View view= LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.fight_item,viewGroup,false);

        /*点击事件*/
        final ViewHolder holder = new ViewHolder(view);
        holder.fightView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Fight fight = mFightList.get(position);
                if (fight.getCurrentState()==2){
                    Toast.makeText(v.getContext(),"您没有权限对已取消的航班进行操作",
                            Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(v.getContext(), ac_to_modifyfight.class);
                    intent.putExtra("FightInfo", fight);
                    view.getContext().startActivity(intent);
                }
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull FightAdapter.ViewHolder viewHolder, int i) {
        Fight fight = mFightList.get(i);

        /*获取出发时间和到达时间*/
        int hour,min,hour1,min1;
        hour=fight.getDATE().getHours();
        hour1=fight.getTODATE().getHours();
        min=fight.getDATE().getMinutes();
        min1=fight.getTODATE().getMinutes();

        int day=fight.getDATE().getDate();
        int day2=fight.getTODATE().getDate();

        /*判断是否要+1天*/
        if(day2>day){
            viewHolder.over24hour.setText("+1天");
        }

        /*设置出发时间和到达时间*/
        String time=String.valueOf(hour)+":"+String.valueOf(min);
        String time1=String.valueOf(hour1)+":"+String.valueOf(min1);
        viewHolder.go_off.setText(time);
        viewHolder.arrival_time.setText(time1);

        /*设置出发机场和到达机场名字*/
        viewHolder.departure.setText(fight.getFROMNAME());
        viewHolder.termini.setText(fight.getTONAME());

        /*设置价格*/
        viewHolder.ac_price.setText("¥"+String.valueOf(fight.getPRICE()));

        /*设置航空公司信息和机型信息*/
        String info = fightToACName(fight.getFLIGHT(),viewHolder)+" "
                +fight.getFLIGHT()+" | "+fight.getAIRCRAFT_MODEL();
        viewHolder.information.setText(info);

        /*设置容量信息*/
        String cap="乘客:"+fight.getCurrentPeole()+"人(最大载客量:"
                +modelToNum(fight.getAIRCRAFT_MODEL())+"人)";
        viewHolder.capacity.setText(cap);

        /*设置航班状态*/
        switch (fight.getCurrentState()){
            case 0: {
                viewHolder.state.setText("准点");
                viewHolder.state.setTextColor(Color.GREEN);
                break;
            }
            case 1:{
                viewHolder.state.setText("延误"+fight.getDelayTime()+"分钟");
                viewHolder.state.setTextColor(Color.parseColor("#FFA500"));
                break;
            }
            case 2:{
                viewHolder.state.setText("取消");
                viewHolder.state.setTextColor(Color.RED);
                break;
            }
        }

    }

    @Override
    public int getItemCount() {
        return mFightList.size();
    }

    /*航班号->航空公司名称*/
    public String fightToACName(String fight,FightAdapter.ViewHolder viewHolder){
        if (fight.contains("MU")){
            viewHolder.log_icon.setImageResource(R.drawable._mu);
            return "东航";
        }else if (fight.contains("CZ")){
            viewHolder.log_icon.setImageResource(R.drawable._cz);
            return "南航";
        }else if (fight.contains("3U")){
            viewHolder.log_icon.setImageResource(R.drawable._3u);
            return "川航";
        }else if (fight.contains("CA")){
            viewHolder.log_icon.setImageResource(R.drawable._ca);
            return "国航";
        }else if (fight.contains("HU")){
            viewHolder.log_icon.setImageResource(R.drawable._hu);
            return "海航";
        }else if (fight.contains("9C")){
            viewHolder.log_icon.setImageResource(R.drawable._9c);
            return "春秋";
        }else {
            viewHolder.log_icon.setImageResource(R.drawable.unknow);
            return "未知航司";
        }
    }


    /*型号->人数*/
    public String modelToNum(String plane){
        switch (plane){
            case "C191(中)": return "168";
            case "空客320(中)": return "180";
            case "空客321(中)": return "180";
            case "空客330(大)": return "350";
            case "波音737(中)": return "160";
            case "波音787(大)": return "400";
            case "庞巴迪900(小)": return "86";
            default:return "未知";
        }
    }
}
